import uilts from '@/utils/index';
/**
 *  任务通用方法
 */
export default {
    data() {
        return {
            uploadBids: [],
            taskLevelList: [
                { value: 1, name: "初级任务" },
                { value: 2, name: "中级任务" },
                { value: 3, name: "高级任务" }
            ],
            osList: [
                { value: '1', name: "安卓" },
                { value: '2', name: "苹果" },
            ],
            filterStatus: null,
            listPage: 1
        }
    },
    created() {
        let queryParams =  this.$route.query;
        if (queryParams.listPage) {
            this.listPage = queryParams.listPage;
        }
        this.initRouterQuery();
    },
    methods: {
        countTaskRadius(surplus,total) {
            total = Number(total);
            if (surplus <= 0) {
                return {
                    achieve: total,
                    total: total,
                    radius: 100,
                    text: `${total}/${total}`
                }
            }
            let achieve = total - surplus;
            let radius = achieve / total * 100;
            // 去除多余的0
            radius = parseFloat(radius.toFixed(2));
            radius = radius;
            return {
                achieve: achieve,
                total: total,
                radius: radius,
                text: `${achieve}/${total}`
            }
        },
        countTrialDate(milliseconds) {
            let date = new Date(milliseconds);
            return uilts.Dateformat(date,"yyyy-MM-dd hh:mm:ss")
        },
        priceFormat(price) {
            return uilts.priceFormat(price,2)
        },
        initRouterQuery() {
            let queryParams =  this.$route.query;
            if (queryParams.page) {
                this.curPage = queryParams.page;
            }
            if (queryParams.searchText) {
                this.searchText = queryParams.searchText;
                if (queryParams.searchType) {
                    this.searchType = queryParams.searchType;
                }
            }
            if (queryParams.filterStatus == 0 || queryParams.filterStatus) {
                this.filterStatus = queryParams.filterStatus;
            }
        },
        /**
         *  修改路由
         */
        updateRoutePar(page) {
            let queryObj = {};
            queryObj.page = page;
            if (this.searchText) {
                queryObj.searchText = this.searchText;
                if (this.searchType) {
                    queryObj.searchType = this.searchType;
                }
            }
            if (this.filterStatus == 0 || this.filterStatus) {
                queryObj.filterStatus = this.filterStatus;
            }
            let queryParams =  this.$route.query || {};
            if (JSON.stringify(queryParams) === JSON.stringify(queryObj)) {
                return;
            }
            let newQuery = Object.assign({},queryParams,queryObj);
            this.$router.push({
                query: newQuery
            })
        },
        getIdListApi(requestFn) {
            let _self = this;
            this.comResquest(requestFn,(res)=>{
                _self.navIdList = res.result;
            },{
                load: true,
                page: this.listPage,
                id_only: 1,
                pagesize: 10
            })
        },
        comQuiteNav(type,curId,cb) {
            let navIdList = this.navIdList;
            let curIndex = navIdList.indexOf(Number(curId));
            let changeId = null;
            //let page = this.listPage;
            let _self = this;
            if (type === 'prve') {
                let prev = curIndex - 1;
                if (prev < 0) {
                    if (this.listPage > 1) {
                        this.listPage = this.listPage - 1;
                        this.switchPage().then((res)=>{
                            _self.navIdList = res.result;
                            changeId =  res.result[res.result.length - 1];
                            cb(changeId);
                        });
                    }else{
                        this.$message.error('已达最开始');
                    }
                    return;
                }
                changeId = navIdList[prev];
            }else{
                let next = curIndex + 1;
                if (next > navIdList.length - 1) {
                    this.listPage = Number(this.listPage) + 1;
                    this.switchPage().then((res)=>{
                        if (res.result.length <= 0) {
                            _self.$message.error('已达最未尾');
                            _self.listPage = _self.listPage - 1;
                        }else{
                            _self.navIdList = res.result;
                            changeId =  res.result[res.result.length - 1];
                            cb(changeId);
                        }
                    });
                    return;
                }
                if (next < navIdList.length) {
                    changeId = navIdList[next];
                }
            }
            cb(changeId);
        },
    },
}